package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import com.mk.cloud.common.domain.PSchemeCode;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.VerifyCodeUtils;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller("schemeCode")
public class PSchemeCodeController extends CookieService {

    @Inject
    private PSchemeCode.Mapper pSchemeCodeMapper;

    @Description("方案--批量插入方案编码")
    @PostAction("addSchemeCodes.json")
    @With({ICLogin.class, ITransacte.class})
    public Integer createSchemeCodes(@Description("方案主键") String sid,@Description("生成的编码数量") Integer codenumber){
        List<String> pscodes = pSchemeCodeMapper.selectPscodesBySid(sid);
        Set<String> set = new HashSet<>(pscodes);
        List<PSchemeCode> list = new ArrayList<>();
        for(int i=0;i<codenumber;i++){
            String code = VerifyCodeUtils.generateVerifyCode(6);
            if(set.add(code)){
                PSchemeCode schemeCode = new PSchemeCode();
                schemeCode.setSid(sid);
                schemeCode.setPscode(code);
                schemeCode.setIsused(GlobalValues.FLAG_0.value());
                schemeCode.setCoid(me.getCoid());
                list.add(schemeCode);
            }else {
                i--;
            }
        }
        pSchemeCodeMapper.insertPSchemeCodesList(list);
        return codenumber;
    }

    @Description("方案--方案编码列表")
    @GetAction("get.json")
    @With({ICLogin.class, ITransacte.class})
    public List<String> getPscodes(@Description("方案主键") String sid){
        List<String> pscodes = pSchemeCodeMapper.selectPscodesBySid(sid);
        return pscodes;
    }


}
